// import axios from 'axios'
import axios from 'axios'
import {
	baseURL
} from '@/config'
// import store from '@/store'

export const request = axios.create({
	baseUrl: baseURL,
	headers: {
		'Content-Type': 'application/json',
		// 'Authorization': uni.getStorageSync('userMsg').token
	},
})

function applyBaseUrl(url, base) {
	return base + url;
}

// XXX: baseUrl not work, use work around
const _get = request.get
const _post = request.post
request.get = (url, config) => {
	return _get(applyBaseUrl(url, baseURL), config)
}
request.post = (url, config) => {
	return _post(applyBaseUrl(url, baseURL), config)
}

request.interceptors.request.use(function(config) {
	// const Storage = {}
	// Storage.get = (name) => {
	// 	return sessionStorage.getItem(name);
	// }
	// config.headers.Authorization = Storage.get('token') ? Storage.get('token') : ''
	// if (!Storage.get('token') || Storage.get('token') === 'undefined') {
	// 	// window.open('http://nmpt.zjamr.zj.gov.cn/zj_promarket_manage_new/login.ftl', '_self')
	// }
	return config;
}, function(error) {
	// Do something with request error
	return Promise.reject(error);
});

// 判断后端接口返回状态
request.interceptors.response.use(
	response => {
		let code
		try {
			code = response.data.code
		} catch (e) {
			console.log(e)
			return Promise.reject(response)
		}

		// if(code == 401){
		//     store.dispatch('userRelogin')
		//     return Promise.reject(response)
		// }
		// if (code !== 0) {
		// 	uni.showToast({
		// 		title: response.data.msg,
		// 		icon: 'none'
		// 	})
		// 	return Promise.reject(response)
		// }

		return response
	},
	error => {
		if (error.response) {
			// switch (error.response.status) {
			//     case 401:
			//         store.dispatch('userRelogin')
			// }
			// uni.showToast({
			// 	title: error.response,
			// 	icon: 'none'
			// })
			return Promise.reject(error.response)
		}
	})
